var e = require;
var t = module;
var o = exports;
var n;
Object.defineProperty(o, "__esModule", {value: !0});
var r = e("SDK"),
    s = e("AdUtils"),
    c = e("Interstitial"),
    l = cc._decorator,
    d = l.ccclass,
    u =
        (l.property,
        (function (e) {
            function t() {
                var t = (null !== e && e.apply(this, arguments)) || this;
                return (
                    (t._isShow = !1),
                    (t._customList = []),
                    (t._showCount = 1),
                    (t._adRequestTime = 0),
                    (t._noADCount = 0),
                    t
                );
            }
            return (
                __extends(t, e),
                (t.prototype.init = function (e) {
                    (this._config = e),
                        (this._isCustom = e.custom_ad_ebable > 0),
                        (this._customCode = e.custom_code),
                        (this._isShowVideoAD = e.icon_ad_enable),
                        this._info || (this._info = qg.getSystemInfoSync());
                }),
                (t.prototype.initCustomTemplate = function () {
                    var e = this,
                        t = new Date().getTime();
                    if (!(0 != this._adRequestTime && t - this._adRequestTime <= 1e3)) {
                        (this._adRequestTime = t), s.default.instance.log("创建Custom广告");
                        var o = this,
                            n = this._info,
                            i =
                                n.screenWidth > n.screenHeight
                                    ? n.screenWidth <= 720
                                        ? n.screenWidth / 2
                                        : n.screenWidth / 2 - 30
                                    : n.screenWidth;
                        s.default.instance.log("创建Custom广告: " + n.screenHeight + "  " + (n.screenHeight - 525));
                        (this._customAd = qg.createCustomAd({
                            posId: o._customCode,
                            style: {top: (n.screenHeight - 750) / 2, left: (n.screenWidth - i) / 2}
                        })),
                            this._customAd.onLoad(function () {
                                s.default.instance.log("原生模板广告加载完成");
                            }),
                            this._customAd.onError(function (e) {
                                s.default.instance.log("原生模板广告加载失败" + JSON.stringify(e));
                            }),
                            this._customAd.onClose(function () {
                                s.default.instance.log("原生模板广告关闭"),
                                    r.default.Instance.reportLog(r.NodeAction.NATIVE_CLOSE, "原生模板广告关闭"),
                                    e.removeCustomBg(),
                                    (e._isShow = !1),
                                    s.default.instance.log("原生模板广告关闭 "),
                                    e._adNode && e._adNode.show_banner && e._adNode.show_banner,
                                    e.customHide();
                            }),
                            s.default.instance.delayShow(function () {
                                e._customAd &&
                                    e._customAd
                                        .show()
                                        .then(function () {
                                            s.default.instance.log("原生模板广告展示完成"),
                                                (e._isShow = !0),
                                                r.default.Instance.reportLog(
                                                    r.NodeAction.NATIVE_SHOW,
                                                    "原生模板广告展示"
                                                ),
                                                e.removeCustomBg(),
                                                s.default.instance.loadPrefabs(
                                                    "sdk/res/prefabs/CustomBg",
                                                    function (t) {
                                                        cc.resources.load(
                                                            "sdk/config/gameConfig",
                                                            cc.JsonAsset,
                                                            function (o, n) {
                                                                if (o) console.error("Failed to load JSON:", o);
                                                                else {
                                                                    var i = r.default.Instance.gameID,
                                                                        a = n.json;
                                                                    (e._customBgNode = cc.instantiate(t)),
                                                                        (e._customBgNode.position.x = 0),
                                                                        (e._customBgNode.position.y = 0),
                                                                        (e._customBgNode.group = a[i - 1].boxGroup),
                                                                        s.default.instance.log(e._customBgNode),
                                                                        s.default.instance.log(
                                                                            "创建Custom广告背景 2024.102.4:" +
                                                                                a[i - 1].boxGroup
                                                                        );
                                                                    var c = cc.director
                                                                        .getScene()
                                                                        .getChildByName("Canvas");
                                                                    s.default.instance.log(c),
                                                                        c.addChild(e._customBgNode, 999, "customAd"),
                                                                        e._customBgNode.getChildByName("closeNode").on(
                                                                            cc.Node.EventType.TOUCH_START,
                                                                            function () {
                                                                                e._customBgNode.removeFromParent();
                                                                            },
                                                                            e
                                                                        );
                                                                }
                                                            }
                                                        );
                                                    }
                                                );
                                        })
                                        .catch(function (t) {
                                            s.default.instance.log("原生模板广告展示失败" + JSON.stringify(t)),
                                                e._adNode &&
                                                    (e._isShowVideoAD
                                                        ? e.scheduleOnce(function () {
                                                              r.default.Instance.showRewardVideo(
                                                                  function () {},
                                                                  function () {
                                                                      r.default.Instance.showToast("广告播放未完成");
                                                                  },
                                                                  function () {
                                                                      r.default.Instance.showToast("暂无广告");
                                                                  }
                                                              );
                                                          }, 1.2)
                                                        : e.scheduleOnce(function () {
                                                              r.default.Instance.showToast("暂无广告");
                                                          }, 1.2));
                                        });
                            }, this._config);
                    }
                }),
                (t.prototype.removeCustomBg = function () {
                    this._customBgNode &&
                        (this._customBgNode.removeFromParent(),
                        this._customBgNode.destroy(),
                        s.default.instance.log("关闭Custom广告背景"));
                }),
                (t.prototype.customShow = function (e) {
                    if (!this._isShow && (this.customHide(), 1 == this._config.enable)) {
                        var t;
                        if (
                            null ==
                                (t =
                                    "string" == typeof this._config.custom_ad_node
                                        ? JSON.parse(this._config.custom_ad_node)
                                        : this._config.custom_ad_node) ||
                            t.length <= 0
                        )
                            s.default.instance.log("---------customShow 未配置广告节点adNodeList为空");
                        else if (((this._adNode = t[e]), null != this._adNode)) {
                            if (this._adNode && 1 != this._adNode.enable) this.showBanner(this._adNode.banner);
                            else {
                                if (
                                    e == c.InterstitialType.INTERSTITIAL_GAME_PAUSE &&
                                    this._showCount < r.default.Instance.inter_show_count
                                )
                                    return this._showCount++, void this.showBanner(this._adNode.banner);
                                e == c.InterstitialType.INTERSTITIAL_GAME_PAUSE &&
                                    this._showCount >= r.default.Instance.inter_show_count &&
                                    (this._showCount = 1),
                                    this.initCustomTemplate();
                            }
                        } else s.default.instance.log("---------customShow 节点未配置广告nodeId：" + e);
                    }
                }),
                (t.prototype.showBanner = function () {}),
                (t.prototype.customHide = function () {
                    this._customAd &&
                        (this.removeCustomBg(),
                        (this._isShow = !1),
                        this._customAd.hide(),
                        this._customAd.destroy(),
                        (this._customAd = null));
                }),
                __decorate([d], t)
            );
        })(cc.Component));
o.default = u;
